﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace MailChimp4Umbraco.Businesslogic.ChimpAPI
{
    public class MailChimpResponse
    {
        /// <summary>
        /// Indicates whether or not the request is successful
        /// </summary>
        public bool IsSuccess { get; set; }
        /// <summary>
        /// If IsSuccess is false, the ErrorCode can be looked up in the MailChimp error code list
        /// </summary>
        public int ErrorCode { get; set; }
        /// <summary>
        /// MailChimps own error message
        /// </summary>
        public string MailChimpErrorMessage { get; set; }

        public MailChimpResponse(string mailChimpResponse)
        {
            if (!string.IsNullOrEmpty(mailChimpResponse))
            {
                XmlDocument chimpResponse = new XmlDocument();
                try
                {
                    chimpResponse.LoadXml(mailChimpResponse);

                    #region Parse XML data

                    XmlNodeList rootElements = chimpResponse.GetElementsByTagName("MCAPI");
                    if (rootElements.Count > 0)
                    {
                        switch (rootElements[0].Attributes["type"].Value.ToLower())
                        {
                            case "boolean":
                                #region Handle response as boolean
                                IsSuccess = true;
                                if (rootElements[0].InnerText == "0")
                                    IsSuccess = false;

                                ErrorCode = 0;
                                MailChimpErrorMessage = string.Empty;

                                #endregion
                                break;

                            case "array":
                                // This type is always an error
                                IsSuccess = false;

                                #region Handle response as array
                                // Run through all child nodes
                                foreach (XmlNode n in rootElements[0].ChildNodes)
                                {
                                    switch (n.Name.ToLower())
                                    {
                                        case "error" :
                                            MailChimpErrorMessage = n.InnerText;
                                            break;

                                        case "code":
                                            ErrorCode = Convert.ToInt32(n.InnerText);
                                            break;
                                    }

                                }

                                #endregion
                                break;

                            default:
                                IsSuccess = false;
                                ErrorCode = -1;
                                MailChimpErrorMessage = string.Empty;
                                break;

                        }
                    } else
                    {
                        throw new InvalidOperationException("Throw the exception just to get to the catch part");
                    }
                    #endregion

                } catch( Exception ex)
                {
                    // If we get here, the string could not be loaded into a xml document
                    IsSuccess = false;
                    ErrorCode = -1;
                    MailChimpErrorMessage = "The response string, could not be loaded as XML or the XML response was incorrectly formated";
                }
            }
        }


    }
}
